/*
 * Copyright © 2022-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springcloud.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NacosController。
 *
 * @author Denvie
 * @date 2022/3/30
 * @since 1.0.0
 */
@RestController
@RequestMapping("/nacos")
public class NacosController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/listAllServices")
    public Map<String, List<ServiceInstance>> listAllService() {
        Map<String, List<ServiceInstance>> resultMap = new HashMap<>(8);
        // 获取注册中心所有服务列表
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            // 获取指定服务的服务器实例列表
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(service);
            resultMap.put(service, serviceInstances);
        }
        return resultMap;
    }
}
